package com.kk.crowd.handler;

import com.kk.crowd.api.MysqlRemoteService;
import com.kk.crowd.constant.CrowdConstant;
import com.kk.crowd.entity.vo.AddressVO;
import com.kk.crowd.entity.vo.MemberLoginVO;
import com.kk.crowd.entity.vo.OrderProjectVO;
import com.kk.crowd.util.ResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderHandler {

    @Autowired
    private MysqlRemoteService mysqlRemoteService;

    private Logger logger = LoggerFactory.getLogger (getClass ());

    // 保存收获地址
    @RequestMapping("/save/address")
    public String saveAddress(AddressVO addressVO,HttpSession session){

        // 1、执行地址信息的保存
        ResultEntity<String> resultEntity = mysqlRemoteService.saveAddressVORemote(addressVO);

        logger.info ("地址保存处理结果："+resultEntity.getResult ());

        // 2、从session域中取得orderProjectVO的对象
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute ("orderProjectVO");

        // 3、从orderProjectVO的对象中取得 returnCount
        Integer returnCount = orderProjectVO.getReturnCount ( );

        // 4、重定向到指定地址，重新进入确认订单页面
        return "redirect:http://www.crowd.com/order/confirm/order/"+returnCount;
    }


    // 显示支持项目的订单详情，渲染
    @RequestMapping("/confirm/order/{returnCount}")
    public String showConfirmOrderInfo(
            @PathVariable("returnCount") Integer returnCount,
            HttpSession session
    ){
        // 1、把接受到的回报数量合并到Session 域
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute ("orderProjectVO");

        orderProjectVO.setReturnCount (returnCount);
        session.setAttribute ("orderProjectVO",orderProjectVO);

        // 2、查询当前已经登录用户id
        MemberLoginVO memberLoginVO = (MemberLoginVO) session.getAttribute (CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
        Integer memberId = memberLoginVO.getId ( );

        // 3、查询目前收获地址数据
        ResultEntity<List<AddressVO>> resultEntity = mysqlRemoteService.getAddressVORemote(memberId);

        if (ResultEntity.SUCCESS.equals (resultEntity.getResult ())){
            List<AddressVO> list = resultEntity.getData ( );

            session.setAttribute ("addressVOList",list);
        }

        return "confirm_order";
    }


    @RequestMapping("/confirm/return/info/{projectId}/{returnId}")
    public String showReturnConfirmInfo(
            @PathVariable("projectId") Integer projectId,
            @PathVariable("returnId") Integer returnId,
            HttpSession session
    ) {
        ResultEntity<OrderProjectVO> resultEntity =
                mysqlRemoteService.getOrderProjectVORemote (projectId, returnId);

        if ("SUCCESS".equals (resultEntity.getResult ( ))) {
            OrderProjectVO orderProjectVO = resultEntity.getData ( );
            session.setAttribute ("orderProjectVO", orderProjectVO);
        }

        return "confirm_return";
    }


}
